/*
    TwoPageView image viewer
    Copyright (C) 2006 Japan Manoretimedia Lab.

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TwoPageView
{
    public partial class ThumbnailCacheSettings : Form
    {
        private ThumbnailCache thumbnailCache;
        private UserPreference userPreference;
        private System.Resources.ResourceManager rm;
        private static System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
        private System.Windows.Forms.Timer updateTimer;
        private Form1 form1;
        private BackgroundDirectoryIterator bdi;
        private const int DISP_UPDATE_INTERVAL_FAST = 33; //< DispUpdate interval (ms)

        public ThumbnailCacheSettings(Form1 form1, ThumbnailCache thumbnailCache, BackgroundDirectoryIterator bdi, UserPreference userPreference)
        {
            if (null == userPreference) {
                throw new System.ArgumentNullException("userPreference");
            }
            InitializeComponent();
            this.form1 = form1;
            this.thumbnailCache = thumbnailCache;
            this.bdi = bdi;
            this.userPreference = userPreference;
            this.rm = Properties.Resources.ResourceManager;
            this.textBoxThumbnailCreateFolder.Text = InitialThumbnailCreateFolder();
            this.checkBoxDispNumberOfThumbnailsInFolderTree.Checked =
                userPreference.DisplayNumberOfThumbnailImagesInFolderTree;
            this.pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);

            DispUpdate();

            updateTimer = new System.Windows.Forms.Timer();
            updateTimer.Interval = DISP_UPDATE_INTERVAL_FAST; //< thumbnailcache status DispUpdate interval (ms)
            updateTimer.Tick += new EventHandler(UpdateTimer_Tick);
            updateTimer.Start();
        }

        private string InitialThumbnailCreateFolder()
        {
            if (form1.SelectedDirInfo != null) {
                string path = form1.SelectedDirInfo.FullName;
                if (System.IO.Directory.Exists(path)) {
                    return path;
                }
            }
            return System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        }

        private void UpdateTimer_Tick(object myObject, System.EventArgs myEventArgs)
        {
            DispUpdate();
        }

        private void DispThreadMonitor()
        {
            Pen pen = new Pen(Color.Black);
            Pen[] pens = new Pen[5];
            pens[0] = new Pen(Color.FromArgb(0xff, 60, 89, 210));
            pens[1] = new Pen(Color.FromArgb(0xff, 68, 130, 232));
            pens[2] = new Pen(Color.FromArgb(0xff, 98, 171, 244));
            pens[3] = new Pen(Color.FromArgb(0xff, 125, 203, 247));
            pens[4] = new Pen(Color.FromArgb(0xff, 182, 238, 250));

            Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            Graphics g = Graphics.FromImage(bmp);

            int rectHeight = pictureBox1.Height / bdi.ChildProcessNum;
            int rectMargin = 0;
            int left = pictureBox1.Width - 1;

            g.DrawImage(pictureBox1.Image, -1, 0);
            g.DrawLine(pen, left, 0, left, pictureBox1.Height);

            for (int y = 0; y < bdi.ChildProcessNum; ++y) {
                BackgroundWorkerWithProcess.Status status = bdi.ChildProcessWorkStatusGet(y);
                if (null == status) {
                    break;
                }
                int top = y * rectHeight;
                int h = rectHeight - rectMargin;
                g.DrawLine(pens[(status.StartTick / 10000) % pens.Length], left, top, left, top+h);
            }
            pictureBox1.Image.Dispose();
            pictureBox1.Image = bmp;
        }

        private void DispUpdate()
        {
            labelThumbnailCacheStatus.Text =
                string.Format(System.Globalization.CultureInfo.InvariantCulture,
                "{0}: {1}\n{2}: {3}\n{4}: {5}\n{6}: {7:F1}",
                rm.GetString("ThumbnailCacheSearchNum"), thumbnailCache.CacheSearchRequestNumber,
                rm.GetString("ThumbnailCacheNum"), thumbnailCache.CacheFileNumber,
                rm.GetString("ThumbnailCacheTooOldNum"), thumbnailCache.TooOldEntryNumber,
                rm.GetString("ThumbnailCacheHitPercent"), (0 == thumbnailCache.CacheSearchRequestNumber) ? 0.0 : 100.0 * thumbnailCache.CacheHitNumber / thumbnailCache.CacheSearchRequestNumber);
            this.textBoxThumbnailCacheFolder.Text = userPreference.ThumbnailCacheFolder;
            if (0 < textBoxThumbnailCreateFolder.Text.Length &&
                System.IO.Directory.Exists(textBoxThumbnailCreateFolder.Text) &&
                !form1.BackgroundRecursiveThumbnailCreaterIsBusy()) {
                this.buttonStartCreateThumbnail.Enabled = true;
            } else {
                this.buttonStartCreateThumbnail.Enabled = false;
            }
            if (form1.BackgroundRecursiveThumbnailCreaterIsBusy()) {
                this.buttonStop.Enabled = true;
            } else {
                this.buttonStop.Enabled = false;
            }

            if (checkBoxThreadMonitor.Checked) {
                DispThreadMonitor();
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void buttonResetThumbnailCache_Click(object sender, EventArgs e)
        {
            thumbnailCache.FileInit();
            DispUpdate();
        }

        private void buttonSelectDifferentLocation_Click(object sender, EventArgs e)
        {
            {
                fbd.Description = rm.GetString("SpecifyThumbnailCacheFolder");
                fbd.SelectedPath = userPreference.ThumbnailCacheFolder;
                System.Windows.Forms.DialogResult dr = fbd.ShowDialog();
                if (dr != System.Windows.Forms.DialogResult.OK) {
                    return;
                }
            }
            System.IO.File.Move(userPreference.ThumbnailCacheFolder + ThumbnailCache.IndexFileName,
                fbd.SelectedPath + ThumbnailCache.IndexFileName);
            System.IO.File.Move(userPreference.ThumbnailCacheFolder + ThumbnailCache.ImageFileName,
                fbd.SelectedPath + ThumbnailCache.ImageFileName);
            thumbnailCache.LoadFromFile(fbd.SelectedPath);
            userPreference.ThumbnailCacheFolder = fbd.SelectedPath;
            DispUpdate();
        }


        private void buttonThumbnailFolderSelect_Click(object sender, EventArgs e)
        {
            {
                fbd.Description = rm.GetString("SpecifyThumbnailCreateFolder");
                fbd.SelectedPath = textBoxThumbnailCreateFolder.Text;
                System.Windows.Forms.DialogResult dr = fbd.ShowDialog();
                if (dr != System.Windows.Forms.DialogResult.OK) {
                    return;
                }
            }
            textBoxThumbnailCreateFolder.Text = fbd.SelectedPath;
            DispUpdate();
        }

        private void buttonStartCreateThumbnail_Click(object sender, EventArgs e)
        {
            buttonStartCreateThumbnail.Enabled = false;
            form1.BackgroundRecursiveThumbnailCacheCreationStart(textBoxThumbnailCreateFolder.Text);
        }

        private void buttonStop_Click(object sender, EventArgs e)
        {
            form1.BackgroundRecursiveThumbnailCacheCreationStop();
        }

        private void checkBoxDispNumberOfThumbnailsInFolderTree_CheckedChanged(object sender, EventArgs e)
        {
            if (userPreference.DisplayNumberOfThumbnailImagesInFolderTree !=
                checkBoxDispNumberOfThumbnailsInFolderTree.Checked) {
                userPreference.DisplayNumberOfThumbnailImagesInFolderTree =
                    checkBoxDispNumberOfThumbnailsInFolderTree.Checked;
                form1.TreeViewRefresh();
            }
        }

        private void checkBoxThreadMonitor_CheckedChanged(object sender, EventArgs e)
        {
            if (!checkBoxThreadMonitor.Checked) {
                pictureBox1.Image.Dispose();
                pictureBox1.Image = new Bitmap(pictureBox1.Width, pictureBox1.Height);
            }
        }
    }
}